﻿using ConfExpert.Forms.Base;
using ConfExpert.Properties;

using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class PasifOnay : XForm
    {
        public PasifOnay()
        {
            InitializeComponent();
        }

        private void PasifOnay_Load(object sender, EventArgs e)
        {
            
            gvWaiting.Init("GetAksPasifOnay", WhoAmI, 5, 0);
            gvConfirmed.Init("GetAksPasifOnay", WhoAmI, 3, 0);
            gvRejected.Init("GetAksPasifOnay", WhoAmI, 4, 0);

            GridManager gm = GridManager.GetManager(gvWaiting);
            GridManager gm2 = GridManager.GetManager(gvConfirmed);
            if (ytk(57))
            {
                gm.GridMenu.AddMenu("Onay", ConfirmDeactivation, Resources.accept_16);
                gm.GridMenu.AddMenu("Red", RejectDeactivation, Resources.delete_16);
                gm2.GridMenu.AddMenu("Değişiklik Notu Ekle", AddNote);
            }
            gm.GridMenu.AddMenu("Stok Hareketleri", gvWaiting_DoubleClick);

            gvWaiting.MakeColumnInvisible("StateId", "SonNotId", "Usable");
            gvConfirmed.MakeColumnInvisible("StateId", "SonNotId", "Usable");
            gvRejected.MakeColumnInvisible("StateId", "SonNotId", "Usable");

            gvWaiting.MakeReadOnly();
            gvConfirmed.MakeReadOnly();
            gvRejected.MakeReadOnly();
            gvWaiting.BestFitColumns();
            gvConfirmed.BestFitColumns();
            gvRejected.BestFitColumns();
            gvWaiting.AddSummary();
            gvConfirmed.AddSummary();
            gvRejected.AddSummary();
            gvWaiting.SetFilterTypeToCheckedList("SonNot");
            gvConfirmed.SetFilterTypeToCheckedList("SonNot");
            gvRejected.SetFilterTypeToCheckedList("SonNot");

            gvWaiting.Columns["NotSayisi"].ColumnEdit = HyperLink();
            gvConfirmed.Columns["NotSayisi"].ColumnEdit = HyperLink();
            gvRejected.Columns["NotSayisi"].ColumnEdit = HyperLink();

            gvWaiting.RowCellClick += gvWaiting_RowCellClick;
            gvConfirmed.RowCellClick += gvConfirmed_RowCellClick;
            gvRejected.RowCellClick += gvRejected_RowCellClick;

            gvWaiting.DoubleClick += gvWaiting_DoubleClick;
            gvConfirmed.DoubleClick += gvWaiting_DoubleClick;
            gvRejected.DoubleClick += gvWaiting_DoubleClick;
        }

        public static RepositoryItemHyperLinkEdit HyperLink()
        {
            RepositoryItemHyperLinkEdit riNoteCount = new RepositoryItemHyperLinkEdit();
            riNoteCount.SingleClick = true;
            return riNoteCount;
        }

        void gvWaiting_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            RowCellClick(gvWaiting, e);
        }

        void gvConfirmed_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            RowCellClick(gvConfirmed, e);
        }

        void gvRejected_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            RowCellClick(gvRejected, e);
        }

        public static void RowCellClick(GridView gv, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if(e.RowHandle > -1)
            if (e.Column.FieldName == "NotSayisi")
                if (gv.GetFocusedDataRow()["NotSayisi"].ToStr() != string.Empty)
                {
                    int aksId = gv.GetFocusedDataRow()["AksesuarId"].ToInt(-1);
                    using (AksPasifNotDetay notDetay = new AksPasifNotDetay(aksId))
                        notDetay.ShowDialog();
                }
        }

        private void RejectDeactivation(object sender, EventArgs e)
        {
            ActivationAction(4); //Aksesuar pasifleştirme talebi RED (stGeriBildirim tablosundan)
        }

        private void ConfirmDeactivation(object sender, EventArgs e)
        {
            ActivationAction(3); //Aksesuar pasifleştirme talebi ONAY (stGeriBildirim tablosundan)
        }

        private void AddNote(object sender, EventArgs e)
        {
            //Onaylanmışlara not ekleme
            string aksIds = gvConfirmed.GetSelectedValues("AksesuarId");
            if (aksIds != String.Empty)
                using (AksesuarNotuEkle ane = new AksesuarNotuEkle())
                {
                    ane.chkRemove.Visible = false;
                    ane.chkUsable.Visible = false;
                    if (ane.ShowDialog() == DialogResult.OK)
                    {
                        ExecuteNonQuery("InsAksesuarPasif", aksIds, WhoAmI, 0, DBNull.Value, ane.note, ane.musteriFaturaDurumu, ane.faturaNo);
                        Guncelle();
                    }
                }
        }

        private void ActivationAction(int state)
        {
            string aksIds = gvWaiting.GetSelectedValues("AksesuarId");
            if (aksIds != String.Empty)
                using (AksesuarNotuEkle ane = new AksesuarNotuEkle())
                {
                    ane.chkRemove.Visible =  false;
                    ane.chkUsable.Visible = false;
                    if (ane.ShowDialog() == DialogResult.OK)
                    {
                        ExecuteNonQuery("InsAksesuarPasif", aksIds, WhoAmI, state, DBNull.Value, ane.note, ane.musteriFaturaDurumu, ane.faturaNo);
                        Guncelle();
                    }
                }
        }

        private void Guncelle()
        {
            gvWaiting.EndEditing();
            gvConfirmed.EndEditing();
            gvRejected.EndEditing();
            bool usable = chkUsable.Checked ? true : false;
            gvWaiting.RefreshDataSource("GetAksPasifOnay", WhoAmI, 5, 0);
            gvConfirmed.RefreshDataSource("GetAksPasifOnay", WhoAmI, 3, usable);
            gvRejected.RefreshDataSource("GetAksPasifOnay", WhoAmI, 4, 0);
            gvWaiting.BestFitColumns();
            gvConfirmed.BestFitColumns();
            gvRejected.BestFitColumns();
        }

        private void gvWaiting_DoubleClick(object sender, EventArgs e)
        {
            int handle = gvWaiting.FocusedRowHandle;

            if (handle > -1)
            {
                int AksesuarId = intParse(gvWaiting.GetRowCellValue(handle, "AksesuarId"));
                using (AksesuarHareketGC ah = new AksesuarHareketGC(AksesuarId, null, null))
                    ah.ShowDialog();
            }
        }

        private void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Guncelle();
        }

        private void chkUsable_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Guncelle();
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (xtraTabControl1.SelectedTabPageIndex == 1)
                chkUsable.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            else
                chkUsable.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }
    }
}